package com.lh.web.service.impl.shop;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.lh.common.entity.admin.Category;
import com.lh.common.entity.admin.Column;
import com.lh.common.enums.ColumnTypeEnum;
import com.lh.common.utils.PageUtils;
import com.lh.web.dao.admin.ColumnMapper;
import com.lh.web.service.serviceAdmin.shop.ColumnService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author FH
 * @ClassName:
 * @Description: (类作用)
 * @date: 2020-06-29 10:58
 */
@Service
public class ColumnServiceImpl extends ServiceImpl<ColumnMapper, Column> implements ColumnService {

    @Override
    public PageUtils queryColumnList(Map<String, Object> param, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Column> page = this.baseMapper.queryColumnList(param);
        return new PageUtils(page);
    }

    @Override
    public Column getColumnName(String name) {
        return this.baseMapper.queryColumnList(name);
    }

    @Override
    @Transactional
    public int addColumn(Column column) {
        Long[] shopId = column.getShopId();
        if(column.getType().equals(ColumnTypeEnum.SORT)){
            column.setSortRelationship(Arrays.toString(shopId));
        }
        int i = this.baseMapper.addColumn(column);
        if(column.getType().equals(ColumnTypeEnum.SORT)){
            this.baseMapper.addColumnShopRef(column.getId(),shopId[shopId.length-1]);
        }else {
            for(int k=0;k<shopId.length; k++){
                this.baseMapper.addColumnShopRef(column.getId(),shopId[k]);
            }
        }
        return i;
    }

    @Override
    @Transactional
    public int updateColumn(Column column) {
        deleteColumnShopRef(column);
        return this.baseMapper.updateColumn(column);
    }

    @Override
    public int updateColumnStatus(Column column) {
        return this.baseMapper.updateColumn(column);
    }

    @Override
    @Transactional
    public int deleteColumn(Long id) {
        this.baseMapper.deleteColumnShopRef(id);
        return this.baseMapper.deleteColumn(id);
    }

    @Override
    public Column getColumnDetails(Long id, ColumnTypeEnum type) {
        Column column = this.baseMapper.selectById(id);
        if(type.equals(ColumnTypeEnum.SORT)){
            List<Map> maps = this.baseMapper.querySort(id);
            column.setMaps(maps);
            return column;
        }else {
            List<Map> maps1 = this.baseMapper.queryColumnDetails(id);
            if(maps1.size() == 0) {
                column.setMaps(maps1);
                return column;
            }
            List<Map> maps = this.baseMapper.queryShop(maps1);
            column.setMaps(maps);
            return column;
        }
    }

    @Override
    public int queryColumnCount() {
        return this.baseMapper.queryColumnCount();
    }

    @Transactional
    public int deleteColumnShopRef(Column column){
        //先删除关系
        this.baseMapper.deleteColumnShopRef(column.getId());

        //再保存关系
        Long[] shopId = column.getShopId();
        if(column.getType().equals(ColumnTypeEnum.SORT)){
            column.setSortRelationship(shopId.toString());
            this.baseMapper.addColumnShopRef(column.getId(),shopId[shopId.length-1]);
        }else {
            for(int k=0;k<shopId.length; k++){
                this.baseMapper.addColumnShopRef(column.getId(),shopId[k]);
            }
        }
        return 0;
    }
}
